/**
 * 账单编辑、新增
 *
 */
define(["app", "js/services/MessageBox", "js/services/MuseService"
    , "js/controllers/transaction/bill/DriverSelectController.js"], function (app) {

    var deps = ["$scope", "$state", "$modal", "$stateParams", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, $stateParams, MessageBox, MuseService) {
        //获取参数
        var id = $stateParams.id || '';

        $scope.createBillForMonth = function () {
            if (!$scope.driver) {
                MessageBox.error("还没选择司机");
                return;
            }

            var param = {
                driverId: $scope.driver.id,
                year: $scope.year,
                month: $scope.month
            };

            MuseService.post(baseServices_restfulUrl + "bill/createBillForMonth.json", param).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        MessageBox.notify('生成账单成功!');
                        $state.go('billList');
                    } else {
                        MessageBox.error('生成账单：' + data.message);
                    }
                })
            .error(function () {
                MessageBox.error('生成账单:服务器正在开小差zZZ');
            });
        }

        //流转订单给司机
        $scope.selectDriver = function () {

            $modal.open({
                templateUrl: 'views/transaction/bill/driverSelect.html',
                controller: 'DriverSelectController',
                backdrop: 'static',
                keyboard: false,
                size: 'lg',
                resolve: {
                    driver: function () {
                        return angular.copy($scope.driver);
                    }
                },
                windowClass: 'app-modal-window'
            }).result.then(function (driver) {
                if (driver) {
                    $scope.driver = driver;
                }
            });
        };

        // 取消，返回
        $scope.close = function () {
            history.back();
        };

        $scope.years = new Array();
        $scope.months = new Array();

        for (var i = 2017; i <= 2099; i++) {
            $scope.years.push({name: i, value: i});
        }

        for (var i = 1; i <= 12; i++) {
            $scope.months.push({name: i, value: i});
        }

        //默认值
        var now = new Date();
        $scope.year = now.getFullYear();
        $scope.month = now.getMonth() + 1;
    }

    controller.$inject = deps;
    app.lazy.controller("CreateBillController", controller);
});
